# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  def show_info
    
    @MESSAGE_TYPES = [:error, :notice, :warning]

    res = ""    
    for name in @MESSAGE_TYPES
      if flash[name]
        res += "<div id=\"#{name}\">#{h(flash[name])}</div>"
      end
    end       
    
    return res
    
  end
  
  def show_user_specific_info
    unless (session[:user_id].nil?) # Show this info only for registered users.
      curr_user = User.find(session[:user_id])
            
      link_to_edit = link_to(curr_user.username, :controller => '/login', :action => :edit)
      link_to_logout = link_to("Logout", :controller => '/login', :action => :logout)
      
      return "<div class = \"user_info\">
                  Hello, #{link_to_edit} | #{link_to_logout}
              </div>"
    end
    
    link_to_login = link_to("Login", :controller => '/login', :action => :login)
    link_to_register = link_to("Register", :controller => '/login', :action => :register)
    
    return "<div class = \"user_info\">
                #{link_to_login} | #{link_to_register}
           </div>"
  end
  
  def show_user_specific_side_bar
    link_to_public = link_to("[&nbsp Public Catalogs &nbsp]", {:controller => '/public', 
                              :action => :catalogs}, :class => "cool_links" )
    link_to_about_us = link_to("About Us", {:controller => '/public', :action => :about_us},
                              :class => "cool_links" )
    
    if(session[:user_id].nil?) # For not registered users.
      return "#{link_to_public} <br /> 
              #{link_to_about_us}"
    else
      user = User.find(session[:user_id])
      link_to_friend_catalogs = link_to("[ Friends Catalogs ]", {:controller => '/user/friend', :action => :list_friend_catalogs},
                                        :class => "cool_links")
                                      
      link_to_mine = link_to("[ Personal Catalog ]", {:controller => '/user/catalog', :action => :list, :id => user.root_catalog.id},
                             :class => "cool_links")
                           
      link_to_friends = link_to("View Friends", {:controller => '/user/friend', :action => :list_your_friends},
                                :class => "cool_links")
                              
      return "#{link_to_public} <br />
              #{link_to_friend_catalogs} <br />
              #{link_to_mine} <br /> <br />
              #{link_to_friends} <br />
              #{link_to_about_us}"
    end
    
  end
  
  def generate(path)
    if path.size == 0
      return ""
    else
      val = link_to('/' + path[0].name, {:controller => "user/catalog", :action => :list, :id => path[0].id}, :class => 'cool_links')
      if (path.size > 1 and path[1].parent != path[0])
        val += " /..."
      end
      for i in (1...(path.size))
        val += ' ' + link_to('/' + path[i].name, {:controller => "user/catalog", :action => :list, :id => path[i].id}, :class => 'cool_links')
      end
      return val
    end
  end
end
